//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation;

namespace EntLibContrib.Resource.Instrumentation
{
	/// <summary>
	/// Defines the logical events that can be instrumented for the resource block.
	/// </summary>
	/// <remarks>
	/// The concrete instrumentation is provided by an object bound to the events of the provider. 
	/// The default listener, automatically bound during construction, is <see cref="ResourceInstrumentationListener"/>.
	/// </remarks>
	[InstrumentationListener(typeof(ResourceInstrumentationListener))]
	public class ResourceInstrumentationProvider
	{
		#region Events
		/// <summary>
		/// Occurs when a <see cref="ResourceManager"/> is created.
		/// </summary>
		[InstrumentationProvider("ResourceInitialized")]
		public event EventHandler<ResourceInitializedEventArgs> ResourceInitialized;

		/// <summary>
		/// Occurs when an item from a <see cref="ResourceManager"/> is retrieved.
		/// </summary>
		[InstrumentationProvider("ResourceAccessed")]
		public event EventHandler<ResourceAccessedEventArgs> ResourceAccessed;

		/// <summary>
		/// Occurs when a failure occurs in a <see cref="ResourceManager"/>.
		/// </summary>
		[InstrumentationProvider("ResourceFailed")]
		public event EventHandler<ResourceFailureEventArgs> ResourceFailed;
		#endregion

		#region Public Methods
		/// <summary>
		/// Fires the <see cref="ResourceInstrumentationProvider.ResourceInitialized"/> event.
		/// </summary>
		[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
		public void FireResourceInitialized (long	totalEntriesCount)
		{
			if ( ResourceInitialized != null )
				ResourceInitialized(this, new ResourceInitializedEventArgs(totalEntriesCount));
		}

		/// <summary>
		/// Fires the <see cref="ResourceInstrumentationProvider.ResourceAccessed"/> event.
		/// </summary>
		/// <param name="key">The key which was used to access the resource.</param>
		[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
		public void FireResourceAccessed(string key)
		{
			if (ResourceAccessed != null)
				ResourceAccessed(this, new ResourceAccessedEventArgs(key));
		}

		/// <summary>
		/// Fires the <see cref="ResourceInstrumentationProvider.ResourceFailed"/> event.
		/// </summary>
		/// <param name="errorMessage">The message that describes the failure.</param>
		/// <param name="exception">The message that represents the exception causing the failure.</param>
		[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
		public void FireResourceFailed(string errorMessage, Exception exception)
		{
			if (ResourceFailed != null)
				ResourceFailed(this, new ResourceFailureEventArgs(errorMessage, exception));
		}
		#endregion
	}
}